package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.dao.BaseJdbcDao;
import jsyf.oa.entity.wreport.WReportGatherEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: @张巨波 @zhangjubo
 * @creat: on 2017/11/28.
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
@Repository
public class WReportGatherDao extends BaseJdbcDao {
    public Integer add(WReportGatherEntity model)
    {
        model.setId(super.generateKey());
        return   super.insert("T_WRT_GATHER",model.getJSONObject());
    }
    public WReportGatherEntity getById(String id){
        String condition = " AND ID = ? ";
        String[] params = {id};
        List<WReportGatherEntity> list = this.getByCondition(condition,params);
        if ( list != null && list.size() > 0 ){
            return list.get(0);
        } else {
            return null;
        }
    }
    public List<WReportGatherEntity> getByCondition(String condition, Object[] params){
        return this.getByPage(condition,params,1,Integer.MAX_VALUE);
    }
    public List<WReportGatherEntity> getByPage(String condition, Object[] params, int requestPage, int pageSize){
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" ID AS id, \n");
        sql.append(" CUSTOMER_NAME AS customerName, \n");
        sql.append(" GATHER_TYPE AS gatherType, \n");
        sql.append(" MONEY AS money, \n");
        sql.append(" BANK AS bank, \n");
        sql.append(" REMARK AS remark, \n");
        sql.append(" W_ID AS wId \n");
        sql.append(" FROM T_WRT_GATHER \n");
        sql.append(" WHERE 1 = 1 \n");
        sql.append(condition);
        sql.append(" ORDER BY SORT_NO ");
        super.appendPageSql(sql,requestPage,pageSize);

        List<JSONObject> jsonList = super.queryForJsonList(sql.toString(),params);

        List<WReportGatherEntity> list = new ArrayList<>();

        for (int i = 0; i < jsonList.size(); i++) {
            WReportGatherEntity  model = JSON.toJavaObject(jsonList.get(i),WReportGatherEntity.class);
            list.add(model);
        }
        return list;
    }
}
